package cn.com.header.core.support.json.fastjson;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import org.springframework.beans.factory.InitializingBean;

import java.util.Map;
import java.util.Objects;

/**
 * FastJson 配置本地化
 * @author liangzhongqiu789@sina.com
 * @date 2017-05-22
 * @time 16:19
 */
public class LocalJsonConfig extends FastJsonConfig implements InitializingBean{

    private Map<Class, AbstractCodec> localCodecMap;


    public void setLocalCodecMap(Map<Class, AbstractCodec> localCodecMap) {
        this.localCodecMap = localCodecMap;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (Objects.nonNull(localCodecMap)) {
            SerializeConfig serializeConfig = getSerializeConfig();
            ParserConfig parserConfig = getParserConfig();
            localCodecMap.forEach((key, value) -> {
                serializeConfig.put(key, value);
                parserConfig.putDeserializer(key, value);
            });
        }
    }

}
